<?
/* @author Udo Schroeter <udo.schroeter@gmail.com>
 * @license dual license: LGPL / commercial use license
 */ 

  // ******** PHP FUNCTIONS **********************

  function CButtonPos($x, $y, $abs = false)
  {
    global $compassPosX, $compassPosY, $compassSize, $compassButtonSize;
    $sizeSpan = $compassSize - $compassButtonSize;
    if ($abs)
    {
      $xofs = $compassPosX;
      $yofs = $compassPosY;
    }
    return('left: '.($xofs+$sizeSpan*$x).'px; top: '.($yofs+$sizeSpan*$y).'px;');
  }

  function getPointsFromCoordRange($coordRange, &$fromPoint, &$toPoint)
  {
    $coords = explode('-', $coordRange);
    // if it's a single coordinate, fill the to-point with the same as the from-point
    if (!isset($coords[1])) $coords[1] = $coords[0];
    $fromPoint = explode(':', $coords[0]);
    $toPoint = explode(':', $coords[1]); 
  }
  
  function gatherCoordRanges(&$intoArray, $prefix, $coordRangeString, $value = 1)
  {
    foreach(explode(',', $coordRangeString) as $coordRange)
    {
      getPointsFromCoordRange($coordRange, $fromPoint, $toPoint);
      for($lx = $fromPoint[0]; $lx <= $toPoint[0]; $lx++)
        for($ly = $fromPoint[1]; $ly <= $toPoint[1]; $ly++)
          $intoArray[] = '"'.$prefix.'_'.$lx.'_'.$ly.'":'.$value;
    }
  }

  // ******** JAVASCRIPT FUNCTIONS **********************

?>
<script>

var playerx = <?= $this->mstats['xpos'] ?>, playery = <?= $this->mstats['ypos'] ?>;
var semaphoreSet = false;
var lastLocation = '-none-';

function SerializeExploredAreas()
{
  var exp = '';
  for(y = 0; y <= <?= $rowCount ?>; y++)
  {
    for(x = 0; x <= <?= $colCount ?>; x++)
    {
      if (exploredAreas['e_'+x+'_'+y])
        exp = exp+'1';
      else
        exp = exp+'0';
    }
    exp = exp+':';
  }
  return(exp);
}

function AccountExploration(x, y, op, opoffset)
{
  // fixme: there is a much easier way to calculate this...
  for(dx = -2; dx <= 2; dx++)
    for(dy = -2; dy <= 2; dy++)
    {
      oplmax = opoffset+Math.abs(Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2))-1)/4;
      if (exploredAreas['e_'+(dx+x)+'_'+(dy+y)] == 1 && op > oplmax) op = oplmax;
    } 
  return(op);
}

function updateMap(px, py)
{
  for(y = 0; y < <?= $rowCount ?>; y++)
  {
    for(x = 0; x < <?= $colCount ?>; x++)
    {
      var cellId = "#cell_"+y+"_"+x;
      if (px == x && py == y)
      {
        $(cellId)
          .addClass('sq_position')
          .removeClass('sq_hidden')
          .css('opacity', 1);
        exploredAreas['e_'+x+'_'+y] = 1;
      }
      else
      {
        var dist = Math.sqrt(Math.pow(px-x, 2) + Math.pow(py-y, 2))-0.5;
        var op = dist/3;
        op = AccountExploration(x, y, op, 0.3); 
        $(cellId)
          .addClass('sq_hidden')
          .css('opacity', op)
          .removeClass('sq_position');
      }
    }
  }
}

function MovePlayer(dx, dy)
{
  if (semaphoreSet) return(false);
  var newx = dx + playerx;
  var newy = dy + playery;
  if (newx >= 0 && newy >= 0 && newx < <?= $colCount?> && newy < <?= $rowCount?>
    && (!impassableAreas['i_'+newx+'_'+newy]))
  {
    playerx = newx;
    playery = newy;
    updateMap(playerx, playery);
    locationId = locationAreas['l_'+playerx+'_'+playery];
    if (locationId) 
    {
      if (lastLocation != locationId)
      {
        semaphoreSet = true;
        $('#locationdiv').fadeTo('fast', 0);
        $('#compassdiv').fadeTo('fast', 0.3);
        $('#loader_img').fadeTo('fast', 1);
        $.get('map/ajax_location/id/'+locationId+'/exp/'+SerializeExploredAreas()+'/l/'+newx+':'+newy, function(data) {
          $('#locationdiv').html(data);
          $('#locationdiv').fadeTo('fast', 1);
          $('#loader_img').fadeTo('fast', 0);
          $('#compassdiv').fadeTo('fast', 1, function(){semaphoreSet = false;});
          });
      }
    }
    else
    {
      $('#locationdiv').fadeTo('fast', 0);
      $.get('map/ajax_update/exp/'+SerializeExploredAreas()+'/l/'+newx+':'+newy);
    }
  }
  lastLocation = locationId;
  $('#bar_posupdate').html(playerx+':'+playery);
}


</script>
<style>
.sq_hidden {
  background: url(img/icons/bgmap100.png) #c0c0c0;
}
.sq_position {
  background: url(img/icons/greenled50.png);
}
#locationdiv {
  position: absolute;
  left: <?= $compassPosX ?>px; 
  top: <?= $compassPosY+$compassSize+10 ?>px;
  padding-left: 8px;
}
#compassdiv {
  position: absolute; 
  left: <?= $compassPosX ?>px; 
  width: <?= $compassSize ?>px; 
  top: <?= $compassPosY ?>px;
  height: <?= $compassSize ?>px;
  background: url('img/icons/compass2.png');
}
.compassbutton,.compasselement {
  position: absolute;
  width: <?= $compassButtonSize ?>px;
  height: <?= $compassButtonSize ?>px;
}
.compassbutton {
  cursor: pointer;
}
#lowerbar {
  position: absolute;
  left: 0px;
  top: 516px;
  width: 550px;
}
#bar_posupdate {
  color: gray;
}
</style>
